package com.jn.exam.paperConsumer.service.impl;


import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import com.jn.exam.paperConsumer.entity.vo.Text;
import com.jn.exam.paperConsumer.schedule.ScheduledTask;
import com.jn.exam.paperConsumer.service.SemanticAnalysisService;
import com.jn.exam.paperConsumer.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 江南大学1033190417
 * @date 2022/2/6 16:10
 */
@Service
public class SemanticAnalysisServiceImpl implements SemanticAnalysisService {

    @Autowired
    private ScheduledTask scheduledTask;

    public Double SemanticAnalysis(Text text) {
        String path = "https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        // 设置header参数
        request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
        // 设置query参数
        request.addQueryParameter("access_token", scheduledTask.getAssess_token());
        request.addQueryParameter("charset", "UTF-8");
        // 设置jsonBody参数
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("text_1", text.getText_1());
        jsonMap.put("text_2", text.getText_2());
        String jsonBody = GsonUtils.toJson(jsonMap);
        request.setJsonBody(jsonBody);
        ApiExplorerClient client = new ApiExplorerClient();
        try {
            ApiExplorerResponse response = client.sendRequest(request);
            // 返回结果格式为Json字符串
            String result = response.getResult();
            Map map = GsonUtils.fromJson(result, Map.class);
            Object score = map.get("score");
            return (Double) score;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Double SemanticAnalysis(String text_1, String text_2) {
        String path = "https://aip.baidubce.com/rpc/2.0/nlp/v2/simnet";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        // 设置header参数
        request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
        // 设置query参数
        request.addQueryParameter("access_token", scheduledTask.getAssess_token());
        request.addQueryParameter("charset", "UTF-8");
        // 设置jsonBody参数
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("text_1", text_1);
        jsonMap.put("text_2", text_2);
        String jsonBody = GsonUtils.toJson(jsonMap);
        request.setJsonBody(jsonBody);
        ApiExplorerClient client = new ApiExplorerClient();
        try {
            ApiExplorerResponse response = client.sendRequest(request);
            // 返回结果格式为Json字符串
            String result = response.getResult();
            Map map = GsonUtils.fromJson(result, Map.class);
            Object score = map.get("score");
            if (score==null){
                return 0.0;
            }
            return (Double) score;
        } catch (Exception e) {
            return 0.0;
        }
    }

    public static void main(String[] args) {
        SemanticAnalysisServiceImpl service = new SemanticAnalysisServiceImpl();
        Double analysis = service.SemanticAnalysis(new Text("五月是憨憨","五月是傻逼"));
        System.out.println("**********************");
        System.out.println(analysis);

    }
}
